package org.macpro.alone.web.taget;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.macpro.alone.server.domain.PageDomain;
import org.macpro.alone.server.util.PageDomainHandle;
import org.macpro.alone.web.common.StringPageParaTool;

public class QueryResultTaget extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		return 0;
	}

	private PageDomain pagedomain;

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		JspWriter jspw = this.pageContext.getOut();
		ServletRequest request = this.pageContext.getRequest();
		// 当前页面记录数据信息
		pagedomain = (PageDomain) request.getAttribute("pagedomain");
		String sortTitle = (String) request.getAttribute("sortTitle");// 排序字段
		String sortType = (String) request.getAttribute("sortType");// 排序类型
		String limitFieldValue= (String)request.getAttribute("limitField");//查询条件值
		if (pagedomain == null) {
			try {
				jspw
						.print("<font class='errorFont'>未生成PageDomain对象，不能产生结果集合</font>");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}
		// 可通用记录集合
		
		List<Map<String, String>> resultList = PageDomainHandle
				.getStringArrayForPageDomain(pagedomain);
		
		// 表体集合
		List<String[]> titleList = TagetUtils.getFormatTitle(title);
		try {
			// *************sortType排序类型
			// *************sortTitle排序字段
			// *************currentPage翻页
			jspw.println("<input type=\"hidden\" id=\"sortTitle_id\" name=\"sortTitle\" value=\""
							+ sortTitle + "\" />");
			jspw.println("<input type=\"hidden\" id=\"limitField_id\" name=\"limitField\" value=\""
					        +limitFieldValue+"\" />");
			jspw.println("<input type=\"hidden\" id=\"sortType_id\" name=\"sortType\" value=\""
							+ sortType + "\" />");
			jspw.println("<table class=\"query_result_body\">");
			jspw.println("<tr>");
			jspw
					.print("<th  style=\"overflow: visible\" ><input onclick=\"alone_checkAll();\" id=\"alone_check_id\" type=\"checkbox\" onclick=\"alone_onclick_allCheck();\" /></th>");
			for (String[] Strtitle : titleList) {
				jspw.println("<th width=\"" + Strtitle[2]
						+ "\"><a onclick=\"alone_queryForSort('" + Strtitle[1]
						+ "');\" >" + Strtitle[0] + "</a></th>");
			}
			jspw.print("</tr>");
			int n=0;
			//行生成
			for (Map<String, String> mapList : resultList) {
				n++;
				jspw.println("<tr  onmouseout=\"alone_mouse_over('"+ mapList.get(id) +"','alone_datacss_"+n%2+"');\" onmousemove=\"alone_mouse_up('"+ mapList.get(id) +"');\"  id=\"alone_tr_id_"
						+ mapList.get(id) +"\" class=\"alone_datacss_"+n%2+"\" >");
				
				jspw.println("<td  style=\"overflow: visible\" ><input name=\"alone_entityCheck\" id=\"row"
						+ mapList.get(id) + "\" type=\"checkbox\"/></td>");
				//列生成
				for (String[] Strtitle : titleList) {
					String value=StringPageParaTool.removeNull(mapList.get(Strtitle[1].toUpperCase())) ;
					jspw.println("<td class=\"alone_datarow_style\"  title=\""+value+"\" onclick=\"alone_getRow('row"+mapList.get(id)+"');\">" + value
							+ "</td>");
				}
				jspw.println("</tr>");
			}
			jspw.println("<tr class='alone_page' >");
			int rownum = titleList.size() + 1;
			jspw.println("<td  class='alone_page'  colspan=\"" + rownum + "\" >");
			jspw.println("<span>" + initPage() + "</span>");
			jspw.println("</td>");
			jspw.println("</tr>");
			jspw.println("</table>");
			//最------后
			jspw.println(endRun());
			// jspw.print();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	private String initPage() {
		int cn = pagedomain.getCrrentPage();// 当前页
		int allpage=pagedomain.getTotalPage();
		int allResult=pagedomain.getTotalResourtSize();
		int sn = cn - 5;//可选开始页
		int en = 0;//可选结束页
		if (sn >= 1) {
			en = cn + 5;
		} else {
			sn = 1;
			en = 10;
		}
		if(en>allpage)en=allpage+1;
		
		StringBuffer returnstr = new StringBuffer();
		returnstr.append("<div class=\"alone_pagelist\">");
		returnstr.append("<span class=\"alone_pageinfo\">共 " + allpage + " 页/" + allResult + "条记录");
		returnstr.append("</span>");
		returnstr.append("<input type=\"hidden\" id=\"currentPage_id\" name=\"currentPage\" />");
		if (pagedomain.getCrrentPage() != 1) {
			returnstr.append("<a class=\"nextPage\" onclick=\"alone_submitForPage('1');\" >首页 </a>");
			int nextPage=cn-1;
			returnstr.append("<a class=\"firstPage\" onclick=\"alone_submitForPage('"+nextPage+"');\" >上页</a>");
		}
	
		for(int n=sn;n<en;n++){
			if(cn==n){
				returnstr.append("<span class=\"alone_current_page\"  >"+n+"</span>");
			}else{
				returnstr.append("<a  onclick=\"alone_submitForPage('"+n+"');\">"+n+"</a>");
			}
		}
		
		if (pagedomain.getCrrentPage() != pagedomain.getTotalPage()) {
			int lastPage=cn+1;
			returnstr.append("<a class=\"nextPage\"  onclick=\"alone_submitForPage('"+lastPage+"');\">下页</a>");
			returnstr.append("<a class=\"endPage\"  onclick=\"alone_submitForPage('"+allpage+"');\" >末页</a>");
		}
		returnstr.append("</div>");
		return returnstr.toString();
	}

	private String title;
	private String id;
	public String endRun(){
		StringBuffer str=new StringBuffer();
		str.append("<script type=\"text/javascript\">alone_init();</script>");
		return str.toString();
	}
	public void setTitle(String title) {
		this.title = title;
	}

	public void setId(String id) {
		this.id = id.toUpperCase();
	}

}
